package com.wpre.booksystem.controller;

import com.wpre.booksystem.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-10-25
 * Time: 16:22
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(@RequestParam String name, @RequestParam String password, HttpSession session) {

        log.info("接收到参数:{}", name);
        //1.校验参数的格式
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
            return false;
        }
        //2.从数据库中校验账号密码是否正确
        Boolean result = userService.checkUserPassword(name, password, session);

        return result;
    }

    @RequestMapping("/register")
    public boolean register(@RequestParam String username, @RequestParam String password, HttpSession session) {

        //1.校验参数的格式
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return false;
        }
        //注册账号
        Boolean result = userService.register(username, password, session);
        return result;
    }
}
